package com.ruoyi.cm.controller;

import java.util.List;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckLogin;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.cm.domain.vo.CmBsContractInfoVo;
import com.ruoyi.cm.domain.bo.CmBsContractInfoBo;
import com.ruoyi.cm.service.ICmBsContractInfoService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 合同管理
 *
 * @author zyb
 * @date 2024-09-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/cm/bsContractInfo")
public class CmBsContractInfoController extends BaseController {

    private final ICmBsContractInfoService iCmBsContractInfoService;

    /**
     * 查询合同管理列表
     */
    @SaCheckLogin
    @GetMapping("/list")
    public TableDataInfo<CmBsContractInfoVo> list(CmBsContractInfoBo bo, PageQuery pageQuery) {
        return iCmBsContractInfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出合同管理列表
     */
    @SaCheckLogin
    @Log(title = "合同管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CmBsContractInfoBo bo, HttpServletResponse response) {
        List<CmBsContractInfoVo> list = iCmBsContractInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "合同管理", CmBsContractInfoVo.class, response);
    }

    /**
     * 获取合同管理详细信息
     *
     * @param contractId 主键
     */
    @SaCheckLogin
    @GetMapping("/{contractId}")
    public R<CmBsContractInfoVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long contractId) {
        return R.ok(iCmBsContractInfoService.queryById(contractId));
    }

    /**
     * 新增合同管理
     */
    @SaCheckLogin
    @Log(title = "合同管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CmBsContractInfoBo bo) {
        return toAjax(iCmBsContractInfoService.insertByBo(bo));
    }

    /**
     * 修改合同管理
     */
    @SaCheckLogin
    @Log(title = "合同管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CmBsContractInfoBo bo) {
        return toAjax(iCmBsContractInfoService.updateByBo(bo));
    }

    /**
     * 查询审批状态
     */
    @SaCheckLogin
    @GetMapping("/getApprovalStatus")
    public R<Map<String, Object>> getApprovalStatus(String bdId) {
        return R.ok(null,iCmBsContractInfoService.getApprovalStatus(bdId));
    }
    /**
     * 删除合同管理
     *
     * @param contractIds 主键串
     */
    @SaCheckLogin
    @Log(title = "合同管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{contractIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] contractIds) {
        return toAjax(iCmBsContractInfoService.deleteWithValidByIds(Arrays.asList(contractIds), true));
    }
}
